class Pay {
  constructor() {
    this.paysers = ['alipay', 'wxpay']
  }

  getPay(provider, orderInfo) {
    return new Promise((resolve, reject) => {
      // 订单信息不能为空
      if (!orderInfo) {
        reject('orderInfo cannot be empty')
      } else {
        // 调起支付
        uni.requestPayment({
          provider,
          orderInfo,
          success: res => {
            resolve(res)
          },
          fail: err => {
            reject(err)
          }
        })
      }
    })
  }

  // 支付宝支付
  alipay(orderInfo) {
    let ali = this.paysers[0]
    return this.getPay(ali, orderInfo)
  }

  // 微信支付
  wxpay(orderInfo) {
    let wx = this.paysers[1]
    return this.getPay(wx, orderInfo)
  }
}

// 初始化实例
let initPay = new Pay()
export default initPay